package de.herzog.hbv;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Pattern;

import de.herzog.bbp.model.Halle;
import de.herzog.bbp.model.Person;
import de.herzog.bbp.model.Verein;

public class VereinTextFileReader {

	private enum Status {
		VEREIN("[A-Z][A-Z][A-Z][A-Z]"), //
		ABTLEITER("A:"), //
		SCHIRIWART("B:"), //
		HALLE("C:"), //
		JUGEND("D:"), //
		MAEDCHEN("E:");

		public Pattern pattern;

		private Status(String patternString) {
			this.pattern = Pattern.compile("^" + patternString + " *\t.*$");
		}
	}

	private static final String TAB = "\t";
	private static final String SEP = "[\t;,]";
	private static final Pattern EMPTY = Pattern.compile("^[ \t]*$");

	private Path inputPath;

	public VereinTextFileReader(Path inputPath) {
		this.inputPath = inputPath;
	}

	private void read() throws IOException {
		BufferedReader reader = Files.newBufferedReader(inputPath, Charset.defaultCharset());
		String line = null;
		Status status = null;
		Verein verein = new Verein();
		Person person = new Person();
		Halle halle = new Halle();
		while ((line = reader.readLine()) != null) {
			if (EMPTY.matcher(line).matches()) {
				continue;
			}
			Status oldStatus = status;
			status = computeStatus(oldStatus, line);
			if (oldStatus != status) {
				if (oldStatus == Status.VEREIN) {
					completeVerein(verein);
				} else if (oldStatus == Status.ABTLEITER || oldStatus == Status.SCHIRIWART) {
					completePerson(person);
				}
			}
			try {
				if (status == Status.VEREIN) {
					readVerein(verein, line);
				} else if (status == Status.ABTLEITER) {
					readPerson(Status.ABTLEITER, verein, person, line);
				} else if (status == Status.SCHIRIWART) {
					readPerson(Status.SCHIRIWART, verein, person, line);
				} else if (status == Status.HALLE) {
					readHalle(verein, halle, line);
				}
			} catch (Throwable e) {
				System.out.println(e);
			}
		}
	}

	private Status computeStatus(Status status, String line) {
		for (Status newStatus : Status.values()) {
			if (newStatus.pattern.matcher(line).matches()) {
				status = newStatus;
			}
		}
		return status;
	}

	private void readVerein(Verein verein, String line) {
		String[] fields = line.split(TAB);
		verein.setName(fields[0].trim());
		verein.setLangName(fields[1].trim());
		verein.setNummer(fields[2].replaceAll(" ", ""));
	}

	private void readPerson(Status status, Verein verein, Person person, String line) {
		String[] fields = line.split(SEP);
		if (status.pattern.matcher(line).matches()) {
			person.setName(fields[1].trim());
			person.setStrasse(fields[2].trim());
			final String ort = fields[3];
			person.setOrt(ort.trim());
		} else {
			for (String field : fields) {
				if (field.length() > 0) {
					if (field.matches(".*@.*")) {
						person.addEmail(field.trim());
					} else {
						person.addTelefon(field.trim());
					}
				}
			}
		}
	}

	private void readHalle(Verein verein, Halle halle, String line) {
		String halleString = line.split(TAB)[1];
		if (halleString != null && halleString.length() > 0) {
			String[] fields = halleString.split(" *- *");
			halle.setVerein(verein);
			halle.setNummer(Integer.valueOf(fields[0].replaceAll("\\. ", "")));
			halle.setAdresse(fields[1]);
			completeHalle(halle);
		}
	}

	private void completeVerein(Verein verein) {
		//System.out.println(verein);
	}

	private void completePerson(Person person) {
		//System.out.println(person);
	}

	private void completeHalle(Halle halle) {
		//System.out.println(halle);
	}

	public static void main(String[] args) throws IOException {
		Path inputPath = Paths.get("H:/My Documents/Privat/Basketball", "Bezirk-Frankfurt-Vereine.txt");
		VereinTextFileReader reader = new VereinTextFileReader(inputPath);
		reader.read();
	}
}
